﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using WXFramework.Model;
using WXFramework;
using Newtonsoft.Json;
namespace CropWXFramework.WebTest.Controllers
{
    public class HomeController : Controller
    {

        private string token { get { return CropToken.GetToken(); } }

        public ActionResult Index()
        {
            return PartialView();
        }

        public ActionResult Content()
        {
            ViewData["CropWXId"] = ConfigurationManager.AppSettings["CropWXId"];
            ViewData["CropWXSecret"] = ConfigurationManager.AppSettings["CropWXSecret"];
            ViewData["CropWXToken"] = ConfigurationManager.AppSettings["CropWXToken"];
            ViewData["CropWXEncodingAESKey"] = ConfigurationManager.AppSettings["CropWXEncodingAESKey"];
            return PartialView();
        }

        public ActionResult Departments(string msg = "")
        {
            if (!string.IsNullOrEmpty(msg))
            {
                var msgresult = JsonConvert.DeserializeObject<WXJsonResultBase>(msg);
                if (!msgresult.IsSucess)
                {
                    ModelState.AddModelError("", msg);
                }
            }

            var jsonresult = CropWXManager.GetDeprtmentList(token);
            var result = new WXDepartmentSitModel();
            result.DepartmentResult = JsonConvert.DeserializeObject<WXGetDepListJsonResult>(jsonresult);
            return PartialView(result);
        }

        public ActionResult UpdateDepartment(WXDepartmentSitModel model)
        {
            var jsonmsg = "";
            if (model.OptType == "add")
            {
                jsonmsg = CropWXManager.CreatDepartment(model.DepModel, token);
            }
            else if (model.OptType == "update")
            {
                jsonmsg = CropWXManager.UpdateDepartment(model.DepModel, token);
            }
            else
            {
                jsonmsg = CropWXManager.DelDepartment(model.DepModel.id.ToString(), token);
            }
            return RedirectToAction("Departments", new { msg = jsonmsg });
        }

        public ActionResult UserInfos(WXUserInfoSiteModel model )
        {
            try
            {
                string jsonstr = "";
                if (!string.IsNullOrEmpty(model.OptType))
                {
                    switch (model.OptType)
                    {
                        case "add":
                            model.UserInfo.department = new List<int>();
                            model.DepartmentIds.Split(',').ToList().ForEach(it => model.UserInfo.department.Add(int.Parse(it)));
                            jsonstr = CropWXManager.CreateUser(model.UserInfo, token);
                            break;
                        case "update":
                            model.UserInfo.department = new List<int>();
                            model.DepartmentIds.Split(',').ToList().ForEach(it => model.UserInfo.department.Add(int.Parse(it)));
                            jsonstr = CropWXManager.UpdateUser(model.UserInfo, token);
                            break;
                        case "del":
                            jsonstr = CropWXManager.DelUser(model.UserInfo.userid, token);
                            break;
                        case "getdepusersinfo":
                            jsonstr = CropWXManager.GetDepUserInfo(model.DepartmentIds, "1", "0", token);
                            model.DepUsersInfo = JsonConvert.DeserializeObject<WXGetDepUsersInfoJsonResult>(jsonstr);
                            break;
                        case "getuserinfo":
                            jsonstr = CropWXManager.GetUserInfo(model.UserInfo.userid, token);
                            model.UserInfo = JsonConvert.DeserializeObject<WXUserInfo>(jsonstr);
                            model.DepartmentIds = string.Join(",", model.UserInfo.department);
                            break;
                    }
                }
                if (!string.IsNullOrEmpty(jsonstr))
                {
                    var jsonresult = JsonConvert.DeserializeObject<WXJsonResultBase>(jsonstr);
                    if (jsonresult.IsSucess)
                    {
                        if (model.OptType != "getdepusersinfo")
                        {
                            model.DepUsersInfo = JsonConvert.DeserializeObject<WXGetDepUsersInfoJsonResult>(CropWXManager.GetDepUserInfo(model.DepartmentIds, "1", "0", token));
                        }
                        if (model.OptType != "getuserinfo")
                        {
                            model.UserInfo = new WXUserInfo();
                            model.OptType = string.Empty;
                            model.DepartmentIds = string.Empty;
                        }

                    }
                    else
                    {
                        ModelState.AddModelError("", jsonstr);
                    }
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }

            return PartialView(model);


        }


       

    }
}